//
//  searchView.m
//  scrollViewAutolayoutTest
//
//  Created by 魏强 on 15/10/30.
//  Copyright © 2015年 魏强. All rights reserved.
//

#import "KDSearchView.h"
#import "KDRGBModel.h"
@interface KDSearchView ()<UITextFieldDelegate>
@property (nonatomic, weak) UIButton *searchBtn;
@property (nonatomic, weak) UITextField *searchTf;


@end
@implementation KDSearchView
- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor clearColor];
        [self setUpSearchBtn];
        [self setUpSearchTf];
    }
    return self;
}
/**
 *  如果想让XIB加载你的自定义的View，那就必须在awakeFromNib方法里面写控件
 */
- (void)awakeFromNib{
    [super awakeFromNib];
    self.backgroundColor = [UIColor clearColor];
    [self setUpSearchBtn];
    [self setUpSearchTf];
    
}

- (void)layoutSubviews{
    [super layoutSubviews];

}
- (void)setReturnSearch:(BOOL)returnSearch{
    _returnSearch = returnSearch;
    _searchTf.returnKeyType = (returnSearch)? UIReturnKeySearch: UIReturnKeyDone;
}
- (NSString *)text{
    _text = self.searchTf.text;
    return _text;
}
- (void)setImageName:(NSString *)imageName{
    _imageName = imageName;
    [_searchBtn setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    
}
- (void)setRadius:(CGFloat)radius{
    _radius = radius;
    [self setNeedsDisplay];
}
- (void)setTextColor:(UIColor *)textColor{
    _textColor = textColor;
    self.searchTf.textColor = textColor;
}
- (void)setPlaceholder:(NSString *)placeholder{
    _placeholder = placeholder;
    UIColor *color = [UIColor lightGrayColor];
    _searchTf.attributedPlaceholder = [[NSAttributedString alloc] initWithString:placeholder attributes:@{NSForegroundColorAttributeName: color, NSFontAttributeName:[UIFont systemFontOfSize:14]}];
    
}
- (void)setUpSearchBtn{
    UIButton *searchBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [searchBtn setImage:[UIImage imageNamed:@"sousuo"] forState:UIControlStateNormal];
//    searchBtn.backgroundColor =RandomColor;
    [searchBtn addTarget:self action:@selector(searchBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:searchBtn];
    self.searchBtn = searchBtn;

}
- (void)setUpSearchTf{
    UITextField * searchTf = [[UITextField alloc]init];
    
    searchTf.returnKeyType = UIReturnKeyDone;
    searchTf.clearButtonMode = UITextFieldViewModeWhileEditing;
    //    KDLog(@"%f", self.width);
    searchTf.delegate = self;
    UIColor *color = [UIColor whiteColor];
    
//    searchTf.placeholder = @";
    searchTf.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"二次检索" attributes:@{NSForegroundColorAttributeName: color}];
//    [searchTf placeholderRectForBounds:CGRectMake((searchTf.bounds.size.width - 52.5) / 2.0, searchTf.bounds.origin.y +3, 105, searchTf.bounds.size.height)];
    searchTf.textColor = [UIColor whiteColor];
    [self addSubview:searchTf];
    self.searchTf = searchTf;
}

- (void)drawRect:(CGRect)rect
{
    CGFloat width = rect.size.width;
    CGFloat height = rect.size.height;
    // 简便起见，这里把圆角半径设置为长和宽平均值的1/10
    CGFloat radius = (width + height) * self.radius;
    
    // 获取CGContext，注意UIKit里用的是一个专门的函数
    CGContextRef context = UIGraphicsGetCurrentContext();
    // 移动到初始点
    CGContextMoveToPoint(context, radius, 0);
    
    // 绘制第1条线和第1个1/4圆弧
    CGContextAddLineToPoint(context, width - radius, 0);
    CGContextAddArc(context, width - radius, radius, radius, -0.5 * M_PI, 0.0, 0);
    
    // 绘制第2条线和第2个1/4圆弧
    CGContextAddLineToPoint(context, width, height - radius);
    CGContextAddArc(context, width - radius, height - radius, radius, 0.0, 0.5 * M_PI, 0);
    
    // 绘制第3条线和第3个1/4圆弧
    CGContextAddLineToPoint(context, radius, height);
    CGContextAddArc(context, radius, height - radius, radius, 0.5 * M_PI, M_PI, 0);
    
    // 绘制第4条线和第4个1/4圆弧
    CGContextAddLineToPoint(context, 0, radius);
    CGContextAddArc(context, radius, radius, radius, M_PI, 1.5 * M_PI, 0);
    
    // 闭合路径
    CGContextClosePath(context);
    // 填充颜色
    if (self.SearchViewBackGroundColor) {
        [self.SearchViewBackGroundColor setFill];
    }else{//默认白色
        CGContextSetRGBFillColor(context, 1.0, 1.0, 1.0, 1.0);
    }
    CGContextDrawPath(context, kCGPathFill);
    
    CGFloat btnWidth = height;
    CGFloat btnHeight = height;
    _searchBtn.width = btnWidth;
    _searchBtn.x = self.width - btnWidth -10;
    _searchBtn.height = btnHeight;
    _searchBtn.y = (self.height - btnHeight) * 0.5;
    if (_searchBtn.currentImage.size.width == 0 ) {
        _searchBtn.width = 0;
        _searchTf.width = self.width;
    }
    CGFloat tfWidth = self.width - self.searchBtn.width;
    _searchTf.x = 10;
    _searchTf.width = tfWidth -10 -10;
    _searchTf.y = 0;
    _searchTf.height = self.height;
}
- (void)searchBtnClick{
    if (_delegate && [_delegate respondsToSelector:@selector(KDSearchViewWithSearchBtnClick:)]) {
        [_delegate KDSearchViewWithSearchBtnClick:self];
    }
}

#pragma mark - UITextFieldDelegate
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if (self.returnSearch) {
        
        [self searchBtnClick];
    }
    [textField resignFirstResponder];
    return YES;
}
- (void)textFieldDidEndEditing:(UITextField *)textField{
     [textField resignFirstResponder];
}
@end
